﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CameraToggle
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        private void btnResetCameraInfo_Click(object sender, EventArgs e)
        {
            txtImgHeight.Text = string.Empty;
            txtImgWidth.Text = string.Empty;
            txtPixelSizeX.Text = string.Empty;
            txtPixelSizeY.Text = string.Empty;
            txtTargetSize.Text = string.Empty;
        }

        private void btnResetLensInfo_Click(object sender, EventArgs e)
        {
            txtVerticalAngle.Text = string.Empty;
            txtHorizonalAngle.Text = string.Empty;
            txtViewHeight.Text = string.Empty;
            txtViewWidth.Text = string.Empty;
            txtViewLength.Text = string.Empty;
            txtFocalLength.Text = string.Empty;
        }

        #region math
        void calByViewLengthForcalLength()
        {
            double imgWidth = Convert.ToDouble(txtImgWidth.Text);
            double pixelX = Convert.ToDouble(txtPixelSizeX.Text);
            double viewWidth = CameraMath.ViewEdgeByPixel(imgWidth, pixelX);

            double imgHeight = Convert.ToDouble(txtImgHeight.Text);
            double pixelY = Convert.ToDouble(txtPixelSizeY.Text);
            double viewHeight = CameraMath.ViewEdgeByPixel(imgHeight, pixelY);

            double forcalLength = Convert.ToDouble(txtFocalLength.Text);

            double viewLength = Convert.ToDouble(txtViewLength.Text);
            double horizontalAngle = CameraMath.Radian2Degree(Math.Atan(viewWidth / 2.0 / forcalLength)) * 2;
            txtHorizonalAngle.Text = string.Format("{0:F3}", horizontalAngle);

            double verticalAngle = CameraMath.Radian2Degree(Math.Atan(viewHeight / 2.0 / forcalLength)) * 2;
            txtVerticalAngle.Text = string.Format("{0:F3}", verticalAngle);

            double tmpViewWidth = Math.Tan(CameraMath.Degree2Radian(horizontalAngle / 2));
            double tagViewWidth = imgHeight * tmpViewWidth * 2;
            txtViewWidth.Text = string.Format("{0:F3}", tagViewWidth);

            double tagViewHeight = tagViewWidth / imgWidth * imgHeight;
            txtViewHeight.Text = string.Format("{0:F3}", tagViewHeight);
        }

        void calByViewLengthAngle()
        {
            double viewLength = Convert.ToDouble(txtViewLength.Text);

            double horizontalAngle = Convert.ToDouble(txtHorizonalAngle.Text);
            double viewWidth = CameraMath.ViewEdge(viewLength, horizontalAngle);
            txtViewWidth.Text = string.Format("{0:F3}", viewWidth);

            double verticalAngle = Convert.ToDouble(txtVerticalAngle.Text);
            double viewHeight = CameraMath.ViewEdge(viewLength, verticalAngle);
            txtViewHeight.Text = string.Format("{0:F3}", viewHeight);

            double imgWidth = Convert.ToDouble(txtImgWidth.Text);
            double imgHeight = Convert.ToDouble(txtImgHeight.Text);
            double pixelY = Convert.ToDouble(txtPixelSizeY.Text);
            double pixelX = Convert.ToDouble(txtPixelSizeX.Text);
            double forcalLength = CameraMath.ForcalLength(viewHeight, viewWidth,
                viewLength, imgWidth, imgHeight, pixelX, pixelY);
            txtFocalLength.Text = string.Format("{0:F3}", forcalLength);
        }
        void calByViewLengthViewSize()
        {
            double viewLength = Convert.ToDouble(txtViewLength.Text);
            double imgWidth = Convert.ToDouble(txtImgWidth.Text);
            double pixelX = Convert.ToDouble(txtPixelSizeX.Text);
            double viewWidth = Convert.ToDouble(txtViewWidth.Text);
            double imgHeight = Convert.ToDouble(txtImgHeight.Text);
            double pixelY = Convert.ToDouble(txtPixelSizeY.Text);
            double viewHeight = Convert.ToDouble(txtViewHeight.Text);

            double forcalLength = CameraMath.ForcalLength(viewHeight, viewWidth,
                viewLength, imgWidth, imgHeight, pixelX, pixelY);
            txtFocalLength.Text = string.Format("{0:F3}", forcalLength);

            double horizonalAngle = (Math.Atan((viewWidth / 2.0) / forcalLength)) * 2.0;
            txtHorizonalAngle.Text = string.Format("{0:F3}", horizonalAngle);

            double vericalAngle = (Math.Atan((viewHeight / 2.0) / forcalLength)) * 2.0;
            txtVerticalAngle.Text = string.Format("{0:F3}", vericalAngle);

        }
        private void activeCal(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    {
                        TextBox txtBox = sender as TextBox;
                        switch (txtBox.Name)
                        {
                            case "txtViewWidth":
                                {
                                    txtViewWidth_Leave(sender, e);
                                }
                                break;
                            case "txtViewHeight":
                                {
                                    txtViewHeight_Leave(sender, e);
                                }
                                break;
                            case "txtHorizontalAngle":
                                {
                                    txtHorizonalAngle_Leave(sender, e);
                                }
                                break;
                            case "txtVerticalAngle":
                                {
                                    txtViewHeight_Leave(sender, e);
                                }
                                break;
                            case "txtViewLength":
                                {
                                    try
                                    {
                                        calByViewLengthViewSize();
                                    }
                                    catch
                                    {
                                        try
                                        {
                                            calByViewLengthAngle();
                                        }
                                        catch
                                        {
                                            try
                                            {
                                                calByViewLengthForcalLength();
                                            }
                                            catch
                                            {
                                            }
                                        }
                                    }
                                }
                                break;
                            case "txtForcalLength":
                                {
                                    try
                                    {
                                        calByViewLengthForcalLength();
                                    }
                                    catch
                                    {
                                    }
                                }
                                break;
                            default:
                                break;
                        }
                        break;
                    }
            }
        }
        #endregion

        private void txtViewWidth_Leave(object sender, EventArgs e)
        {
            try
            {
                double viewWidth = Convert.ToDouble(txtViewWidth.Text);
                double imgHeight = Convert.ToDouble(txtImgHeight.Text);
                double imgWidth = Convert.ToDouble(txtImgWidth.Text);
                double viewHeight = CameraMath.ViewEdge(viewWidth, imgWidth, imgHeight);
                txtViewHeight.Text = string.Format("{0:F3}", viewHeight);
                if (txtViewLength.Text != string.Empty)
                {
                    calByViewLengthViewSize();
                }
            }
            catch { }
        }

        private void txtViewHeight_Leave(object sender, EventArgs e)
        {
            try
            {
                double viewHeight = Convert.ToDouble(txtViewHeight.Text);
                double imgHeight = Convert.ToDouble(txtImgHeight.Text);
                double imgWidth = Convert.ToDouble(txtImgWidth.Text);
                double viewWidth = CameraMath.ViewEdge(viewHeight, imgHeight, imgWidth);
                txtViewWidth.Text = string.Format("{0:F3}", viewWidth);
                if (txtViewLength.Text != string.Empty)
                {
                    calByViewLengthViewSize();
                }
            }
            catch { }

        }

        private void txtHorizonalAngle_Leave(object sender, EventArgs e)
        {
            try
            {
                double imgHeight = Convert.ToDouble(txtImgHeight.Text);
                double imgWidth = Convert.ToDouble(txtImgWidth.Text);
                double horizontalAngle = Convert.ToDouble(txtHorizonalAngle.Text);
                double verticalAngle = CameraMath.ViewAngle(horizontalAngle, imgHeight, imgWidth);
                txtVerticalAngle.Text = string.Format("{0:F3}", verticalAngle);
                if (txtViewLength.Text != string.Empty)
                {
                    calByViewLengthAngle();
                }
            }
            catch
            {
            }

        }
        private void txtVerticalAngle_Leave(object sender, EventArgs e)
        {
            try
            {
                double imgHeight = Convert.ToDouble(txtImgHeight.Text);
                double imgWidth = Convert.ToDouble(txtImgWidth.Text);
                double verticalAngle = Convert.ToDouble(txtVerticalAngle.Text);
                double horizontalAngle = CameraMath.ViewAngle(verticalAngle, imgWidth, imgHeight);
                txtHorizonalAngle.Text = string.Format("{0:F3}", horizontalAngle);
                if (txtViewLength.Text != string.Empty)
                {
                    calByViewLengthAngle();
                }
            }
            catch { }
        }
    }
}